<?php
    class admin_addAutoProducts_controller extends controllerBase
    {
        public function process()
        {
            $oDatabase = new database();
            $aSettings = database::getListSettings('add_product');

            $sQuery = "SELECT * FROM product_list_url WHERE status = 0 ORDER BY id ASC LIMIT 1";
            $oLinkProduct = $oDatabase->getObject($sQuery);
            if($oLinkProduct)
            {
                $sQuery = "SELECT * FROM provider WHERE id='".$oLinkProduct->provider_id."'";
                $oProvider = $oDatabase->getObject($sQuery);
                // Create DOM from URL
                $html = file_get_html($oLinkProduct->url);

                // Find all article blocks
                foreach($html->find($oProvider->title_sector) as $article) {
                    $sTitle = $article->plaintext;
                }

                foreach($html->find($oProvider->price_sector) as $article) {
                    $sPrice = preg_replace('/\D/', '', $article->plaintext);
                }

                foreach($html->find($oProvider->description_sector) as $article) {
                    $sDescription = $article->plaintext;
                }

                $i = 0;
                $aImages = array();
                foreach($html->find($oProvider->image_sector) as $article) {
                    $sFilenameIn  = trim($article->src);
                    $sFilenameIn = URL::convertLink($sFilenameIn);
                    $temp = explode('/',$sFilenameIn);
                    $sFileName = end($temp);
                    $sFilenameOut = 'upload/'.$sFileName;

                    $contentOrFalseOnFailure   = file_get_contents($sFilenameIn);
                    $byteCountOrFalseOnFailure = file_put_contents($sFilenameOut, $contentOrFalseOnFailure);

                    $aImages[] = $sFileName;
                    $i+=1;
                    if($i >= $aSettings['default_number_image_download']['setting_value'])
                        break;
                }
                $sUserUri = $aSettings['default_user_add_product']['setting_key'];
                $sCategoryUri = $oProvider->category_uri;
                $sCategoriesUri = $oProvider->categories_uri;
                $sMenufacturerUri = $aSettings['default_menufacturer_add_product']['setting_key'];
                $sLocation = $aSettings['default_location_add_product']['setting_value'];
                $sProductId = DBObject::addProduct($sUserUri,$sTitle,$sPrice,$sDescription,$aImages,$sCategoryUri,$sCategoriesUri,$sMenufacturerUri,$sLocation);
                $aData = array(
                    'status' => 1,
                );
                $aCondition = array(
                    "id = '".$oLinkProduct->id."'" => '',
                );

                $oDatabase->update('product_list_url',$aData,$aCondition);
                $this->aParams['new_product_uri'] = $sProductId;
                if($aSettings['default_status_add_product']['setting_value'] == 'Auto')
                {
                    $this->aParams['bIsAuto'] = true;
                }
            }
        }
    }
?>
